package Test_1110;

import Test_1106.MySingleList;

import java.util.List;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class MyLinkedList implements IList{
    class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
         ListNode cur=new ListNode(data);
         if(head==null){
             head=last=cur;
             return;
         }
         head.prev=cur;
         cur.next=head;
         head=cur;
    }
    @Override
    public void addLast(int data) {
        ListNode cur=new ListNode(data);
        if(head==null){
            head=last=cur;
            return;
        }
        cur.prev=last;
        last.next=cur;
        last=cur;
    }
    @Override
    public void addindex(int index, int data) {
        int len=size();
        ListNode cur=head;
        ListNode node=new ListNode(data);
        if(index<0 || index>len){
            System.out.println("插入位置不合法");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==len){
            addLast(data);
            return;
        }
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        node.next=cur.next;
        cur.next.prev=node;
        node.prev=cur;
        cur.next=node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    //不严谨
  /*  public void remove(int key) {
        ListNode node=new ListNode(key);
        ListNode cur=head;
        if(head==null){
            return;
        }
        if(!contains(key)){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        while(cur.next.val!=key){
            cur=cur.next;
        }
        node=cur.next;
        cur.next=node.next;
        node.next.prev=node.prev;

    }*/
    public void remove(int key) {
        //判断是否为头结点
        //判断是否头结点的下一个结点为null；
        //判断是否为尾节点
        ListNode node=new ListNode(key);
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head==null){
                        head.prev=null;
                    }
                }
            }else{
                cur.prev.next=cur.next;
                cur.next.prev=cur.prev;
                if(cur.next==null){
                    last=last.prev;
                }else{
                    cur.next.prev=cur.prev;
                }
            }
            return;
        }
        cur=cur.next;
    }
    @Override
    public void removeAllKey(int key) {
        ListNode node=new ListNode(key);
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head==null){
                        head.prev=null;
                    }
                }
            }else{
                cur.prev.next=cur.next;
                cur.next.prev=cur.prev;
                if(cur.next==null){
                    last=last.prev;
                }else{
                    cur.next.prev=cur.prev;
                }
            }
        }
        cur=cur.next;
    }


    @Override
    public void clear() {
        ListNode cur=head;
        if(head==null){
            return;
        }
        ListNode curN=null;
        while(cur!=null){
            curN=cur.next;
            cur.next=null;
            cur.prev=null;
            cur=curN;
        }
        head=last=null;
    }
    @Override
    public int size() {
        int len=0;
        ListNode cur=head;
        while(cur!=null){
            cur=cur.next;
            len++;
        }
        return len;
    }
    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
}
